package com.simba.system.controller;

import com.simba.response.Result;
import com.simba.system.entity.AuthUser;
import com.simba.system.entity.User;
import com.simba.system.service.UserService;
import com.simba.utils.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * <p>
 * 登录控制器
 * </p>
 *
 * @author 陈俊 1002471284@qq.com
 * @since 2021-03-24
 */
@Api(tags = "LoginController")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public Result login(@RequestBody AuthUser authUser, HttpServletRequest request) {
        System.out.println("进入了loginController！");
        return userService.login(authUser.getUsername(), authUser.getPassword(), authUser.getCode(), request);
    }

    @ApiOperation(value = "获取当前用户信息")
    @PostMapping("/getUserInfo")
    public Result getUserInfo(Principal principal) {
        if (null == principal) {
            return Result.error().message("principal为空");
        }
        User user = userService.getUserByUsername(principal.getName());
        user.setPassword(null);
        user.setRoles(userService.getRoles(user.getUserId()));
        return Result.success().data("userinfo", user);

    }

    @ApiOperation(value = "退出用户登录")
    @PostMapping("/logout")
    public Result logout() {
        return Result.success().message("注销成功！");
    }

    @ApiOperation(value = "测试接口")
    @PostMapping("/hello")
    public Result hello() {
        return Result.success().message("hello,welcome to ocean!");
    }

    @ApiOperation(value = "admin可以访问")
    @PostMapping("/hello_admin")
    public Result adminHello() {
        return Result.success().message("hello admin,welcome to ocean!");
    }

    @ApiOperation(value = "common可以访问")
    @PostMapping("/hello_common")
    public Result commonHello() {
        return Result.success().message("hello common,welcome to ocean!");
    }
}
